package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import Function.FCMutilPlayCache;
import VWorldGameStudio.*;
import VWorldGameStudio.events.MapHiddenEvent;
import lombok.Setter;

import java.util.ArrayList;

import static FCRendering.FCRMaster.*;
import static FCRendering.FCRMaster.player1;

public class FunctionTanCheResult extends AShopFuntion {

    private ArrayList<FixTank> serviceList = new ArrayList<>();

    ArrayList<MapHiddenEvent> hiddenEvents;
    ArrayList<String> xyKeys;
    private String rItem;
    private int showFristIndex = 0;

    @Setter
    private int endI = 0;
    private int selectIndex = 0;

    public void reBuildWindow(String randItem, ArrayList<MapHiddenEvent> hiddenEvents, ArrayList<String> xyKeys) {
        this.hiddenEvents = hiddenEvents;
        this.xyKeys = xyKeys;
        rItem = randItem;

        setEndI(Math.min(serviceList.size(), showFristIndex + 3));
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        FCRValues.leftWindowUI[0].leftText = "探测器探测到物品:";
        if (rItem != null) {
            FCItem item = FCRMaster.itemLib.getItemOfID(rItem);
            FCRValues.leftWindowUI[1].leftText = item.getChName();
            FCRValues.leftWindowUI[1].rightText = item.getWeight() + "t";
        } else {
            int l = hiddenEvents.size();
            l = Math.min(5, l);
            for (int i = 1; i < l + 1; i++) {
                FCItem item = FCRMaster.itemLib.getItemOfID(hiddenEvents.get(i - 1).getHiddenitemId());
                FCRValues.leftWindowUI[i].leftText = item.getChName();
                FCRValues.leftWindowUI[i].rightText = item.getWeight() + "t";
            }
        }

        if (selectIndex > 0) {
            int m = 0;
            for (int i = showFristIndex; i < endI; i++) {
                FCRValues.rightWindowUI[m].leftText = serviceList.get(i).text;
                m++;
            }
        }

        FCRValues.rightWindowUI[3].rightText = "上一页";
        FCRValues.rightWindowUI[4].rightText = "下一页";
        FCRValues.rightWindowUI[5].rightText = "留原地退出";
    }

    private void getPlayerAndTank(boolean onlyTank) {
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if (!onlyTank) {
            serviceList.add(new FixTank(player1, "放到 " + player1.getChName() + " 包里"));
        }
        if (tank != null) {
            serviceList.add(new FixTank(tank, "放到 " + langLib.getNameOfId(tank.getName()) + " 上"));
        }
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if (link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, link.curPlayerName);
                tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                if (!onlyTank) {
                    serviceList.add(new FixTank(p, "放到 " + p.getChName() + " 包里"));
                }
                if (tank != null) {
                    serviceList.add(new FixTank(tank, "放到 " + langLib.getNameOfId(tank.getName()) + " 上"));
                }
            }
        }
        //牵引的

        for (FCPropulsion fc : fcv.getPropulsions()) {
            if (fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")) {
                serviceList.add(new FixTank((FCTank) fc, "放到 " + langLib.getNameOfId(fc.getPropulsionName()) + " 上"));
            }
        }

    }


    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if (entity.leftText.length() > 1) {
            if (i > 0) {
                selectIndex = i;
                serviceList.clear();
                FCItem selectItem = null;
                if (rItem != null) {
                    selectItem = FCRMaster.itemLib.getItemOfID(rItem);
                } else {
                    selectItem = FCRMaster.itemLib.getItemOfID(hiddenEvents.get(selectIndex - 1).getHiddenitemId());
                }
                if (selectItem.getCatalog().equals("tank")) {
                    getPlayerAndTank(true);
                } else {
                    getPlayerAndTank(false);
                }
                setEndI(Math.min(serviceList.size(), showFristIndex + 3));
                resetUI();
            }
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        if (i == 5) {
            fcv.setOpenTanCheResult(false);
        } else if (i == 3) {
            if (showFristIndex >= 3) {
                showFristIndex -= 3;
                setEndI(Math.min(serviceList.size(), showFristIndex + 3));
                resetUI();
            }
        } else if (i == 4) {
            if (endI < serviceList.size()) {
                showFristIndex += 3;
                setEndI(Math.min(serviceList.size(), showFristIndex + 3));
                resetUI();
            }
        } else if (entity.leftText.length() > 1) {
            FixTank select = serviceList.get(i);
            boolean putOk = false;
            String itemName = rItem;
            if (rItem == null) {
                itemName = hiddenEvents.get(selectIndex - 1).getHiddenitemId();
            }
            if (itemName != null) {
                if (select.playerInfo != null) {
                    //放人类背包
                    putOk = select.playerInfo.addItemToKnapsack(itemName);
                } else {
                    putOk = select.tank.addItemToKnapsack(itemName);
                    select.tank.reInitShuxing();
                }
            }
            serviceList.clear();
            if (putOk) {
                if (rItem != null) {
                    fcv.setOpenTanCheResult(false);
                } else {
                    gameSave.getHiddenItemWasTakeList().add(xyKeys.get(selectIndex - 1));
                    xyKeys.remove(selectIndex - 1);
                    hiddenEvents.remove(selectIndex - 1);
                    if (hiddenEvents.size() > 0) {
                        showFristIndex = 0;
                        setEndI(Math.min(serviceList.size(), showFristIndex + 3));
                        resetUI();
                        FCRValues.rightWindowUI[0].leftText = "物品已放好，请继续";
                    } else {
                        fcv.setOpenTanCheResult(false);
                    }
                }
            } else {
                fcv.getMessage().add("背包已满，放不下" + itemLib.getItemOfID(itemName).getChName() + "了");
                fcv.showMessage();
            }
        }
    }
}
